//
//  SWPackageChooseViewController.swift
//  ChildStory
//
//  Created by linhan on 2017/1/14.
//  Copyright © 2017年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWPackageChooseViewController: SWBaseViewController,UIWebViewDelegate,ScrollTabBarDelegate
{
    var chooseCallback:((Int)->Void)?
    
    private var _webView:UIWebView?
    private var _tabBar:ScrollTabBar?
    private var _indicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray)
    
    private var _selectedIndex:Int = 0
    private var _packages:[SWProductPackage] = []
    
    private var _url:String = ""{
        didSet{
            if let url = URL(string: _url)
            {
                _webView?.loadRequest(URLRequest(url: url))
            }
        }
    }
    
    init(packages:[SWProductPackage], selectedIndex:Int)
    {
        super.init(nibName: nil, bundle: nil)
        _packages = packages
        _selectedIndex = selectedIndex
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }
    
    private func setup()
    {
        view.backgroundColor = UIColor.white
        
        automaticallyAdjustsScrollViewInsets = false
        
        let topBarHeight:CGFloat = createTabBar(y: navigationBarBottom, container: view)
        let chooseBtnHeight:CGFloat = 50
        let chooseBtn:UIButton = UIButton(type: .system)
        chooseBtn.setTitle("选择", for: .normal)
        chooseBtn.frame = CGRectMake(0, view.height - chooseBtnHeight, view.width, chooseBtnHeight)
        chooseBtn.backgroundColor = UIColor.lightGray
        chooseBtn.addTarget(self, action: #selector(chooseBtnTapped), for: .touchUpInside)
        view.addSubview(chooseBtn)
        
        let webViewRect:CGRect = CGRectMake(0, topBarHeight, view.width, view.height - topBarHeight - chooseBtnHeight)
        _webView = UIWebView(frame: webViewRect)
        _webView?.delegate = self
        view.addSubview(_webView!)
        
        _indicatorView.center = view.center
        view.addSubview(_indicatorView)
        
        if let url = _packages.valueAt(_selectedIndex)?.detailURL
        {
            _url = url
        }
    }
    
    
    
    private func createTabBar(y:CGFloat, container:UIView) -> CGFloat
    {
        let tabBar:ScrollTabBar = ScrollTabBar(frame: CGRectMake(0, y, container.width, 40))
        tabBar.font = UIFont.systemFont(ofSize: 14)
        tabBar.itemSpacing = 30
        tabBar.alignment = .center
        tabBar.selectedColor = UIColor(hex: 0x2f3231)
        tabBar.unselectedColor = UIColor(hex: 0xababab)
        tabBar.delegate = self
        tabBar.titleItems = _packages.map({$0.title})
        tabBar.selectedIndex = _selectedIndex
        container.addSubview(tabBar)
        _tabBar = tabBar
        return tabBar.bottom
    }
    
    func webViewDidStartLoad(_ webView: UIWebView)
    {
        _indicatorView.startAnimating()
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView)
    {
        _indicatorView.stopAnimating()
    }
    
    func webView(_ webView: UIWebView, didFailLoadWithError error: Error)
    {
        _indicatorView.stopAnimating()
    }
    
    func scrollTabBar(_ tabBar: ScrollTabBar, didSelectIndex index: Int)
    {
        _selectedIndex = index
        if let url = _packages.valueAt(index)?.detailURL
        {
            _url = url
        }
    }
    
    @objc private func chooseBtnTapped()
    {
        chooseCallback?(_selectedIndex)
        navigationController?.popViewController(animated: true)
    }
    
}
